package com.yzw.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author yzw
 * @date 2022/1/23 16:25
 * @desc redis工具类
 */
@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private Jedis jedis;

    /**
     * 设置key value
     * @param key
     * @param value
     * @return
     */
    public boolean set(final Object key, Object value){
        if(null == key){
            return true;
        }
        redisTemplate.opsForValue().set(key, value);
        return true;
    }

    public synchronized boolean setnx(final Object key, Object value){
        if(null == key){
            return true;
        }
        return redisTemplate.opsForValue().setIfAbsent(key, value);
    }

    /**
     * 设置key value 过期时间
     * @param key
     * @param value
     * @param seconds
     * @return
     */
    public boolean setExpire(final Object key, Object value, Long seconds){
        if(null == key){
            return true;
        }
        redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
        return true;
    }

    /**
     * 设置过期时间，如果存在就返回失败
     * @param key
     * @param value
     * @param seconds
     * @return
     */
    public synchronized boolean setnxExpire(final Object key, Object value, Long seconds){
        if(null == key){
            return true;
        }
        return redisTemplate.opsForValue().setIfAbsent(key, value, seconds, TimeUnit.SECONDS);
    }

    public String blpop(){
        RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
        List<byte[]> bytes = connection.bLPop(10, "yzw".getBytes());
        if (null != bytes && bytes.size() == 2) {
            return new String(bytes.get(1), StandardCharsets.UTF_8);
        }
        return null;
    }

    public boolean rpush(String key, String value){
        RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
        Long rPushResult = null;
        try {
            rPushResult = redisConnection.rPush(key.getBytes(StandardCharsets.UTF_8), value.getBytes());
            return null != rPushResult && rPushResult > 0;
        } catch (Exception e) {
            System.out.println("异常：" + e.getMessage());
            return false;
        } finally {
            redisConnection.close();
        }
    }

    public Object lpop(){
        Object o = redisTemplate.opsForList().leftPop("yzw");
        //jedis.blpop()
        return o;
    }

    /**
     * 解决统计问题  HyperLogLog
     * @param key
     * @param value
     */
    public void pfadd(String key, String value){
        jedis.pfadd(key,value);
    }

    public long pfcount(String key){
        long pfcount = jedis.pfcount(key);
        return pfcount;
    }

    /**
     * 添加该key下某个值的权重
     * @param key
     * @param score
     * @param value
     */
    public void zadd(String key, Long score, String value){
        Long zadd = jedis.zadd(key, score, value);
    }

    /*public void zremrangeByScore(String key, Long score, String value){
        jedis.zremrangeByScore(key, score, value);
    }*/
}
